<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\CourseModel;
use app\model\CoursesReviewsModel;
use app\model\CoursesUsersLikeModel;
use app\model\UserModel;
use think\App;
use think\facade\Db;

class CoursesReviews extends Base {

    private $userModel;

    private $req;

    private $uid;

    private $coursesReviewsModel;

    private $courseModel;

    private $orderType ;

    private $page;

    private $limit = 20;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page']  : 1;

        $this->orderType = isset($this->req['orderType']) ? $this->req['orderType']  :  "created_at";

        $this->courseModel = new CourseModel();

        $this->userModel = new UserModel();

        $this->coursesReviewsModel = new CoursesReviewsModel();

        $this->uid = $this->isLogin();

    }


    public function getCourseReviewList()
    {

        if(!$this->req["course_id"]){
            return $this->fail("请选择课程");
        }

        $where =[];
        $where[] = ['course_id',"=",$this->req["course_id"]];
        $where[] = ['p_course_review_id','=',0];
        $where[] = ['review_status','=',1];

        $list = $this->coursesReviewsModel->getList($where,$this->page,$this->limit,$this->orderType,"DESC");

        $coursesUsersLikeModel = new CoursesUsersLikeModel();
        foreach ($list as $key=>&$item){
            $item["reply_num"] = $this->coursesReviewsModel->getTotal([
                ['p_course_review_id','=',$item["course_review_id"]],
                ['review_status','=',1],
            ]);

            $item["is_like"] = $coursesUsersLikeModel->isLike($item["course_review_id"],$this->uid);
        }

        $total = $this->coursesReviewsModel->getTotal($where);

        return $this->succData($total,$list);
    }


    public function getCourseReviewChildList()
    {

        if(!$this->req["course_review_id"]){
            return $this->fail("请选择评价");
        }

        $where =[];
        $where[] = ['p_course_review_id','=',$this->req["course_review_id"]];
        $where[] = ['review_status','=',1];

        $list = $this->coursesReviewsModel->getList($where,$this->page,$this->limit,$this->orderType,"DESC");

        $coursesUsersLikeModel = new CoursesUsersLikeModel();
        foreach ($list as $key=>&$item){
            $item["reply_num"] = $this->coursesReviewsModel->getTotal([
                ['p_course_review_id','=',$item["course_review_id"]],
                ['review_status','=',1],
            ]);

            $item["is_like"] = $coursesUsersLikeModel->isLike($item["course_review_id"],$this->uid);
        }

        $total = $this->coursesReviewsModel->getTotal($where);

        return $this->succData($total,$list);
    }
    /*
     * 商品评价
     *
     * */
    public function publishComments(){

        if(!$this->req["course_id"]){
            return $this->fail("请选择要评价的课程");
        }

        $courseInfo = $this->courseModel->getInfo($this->req["course_id"]);
        if(!$courseInfo){
            return $this->fail("课程不存在");
        }

        if(!$this->req["review_content"]){
            return $this->fail("请评价下课程吧~");
        }

        $userInfo = $this->userModel->getUserInfoByUid($this->uid);
        $userBaseInfo = [
            'uid' => $userInfo["uid"],
            'username' => $userInfo["username"],
            'mobile' => $userInfo["mobile"],
            'gender' => $userInfo["gender"],
            'avatar' => $userInfo["avatar"],
        ];

        if(isset($this->req["review_content"]) && !empty($this->req["review_content"])){
            $this->req["review_content"] = base64_encode($this->req["review_content"]);
        }
        $data = [
            "uid"=>$this->uid,
            "course_id"=>$this->req["course_id"],
            "user_info"=>json_encode($userBaseInfo,256),
            "review_content"=>  $this->req["review_content"],
            "review_thumbnail"=> (isset($this->req["review_thumbnail"]) && is_array($this->req["review_thumbnail"])) ? json_encode($this->req["review_thumbnail"],256) : "",
            "review_status"=>1,
            "p_course_review_id"=>isset($this->req["p_course_review_id"]) && !empty($this->req["p_course_review_id"]) ? $this->req["p_course_review_id"] : 0,
        ];

        if(isset($this->req["p_course_review_id"]) && !empty($this->req["p_course_review_id"])){
            $pCourseReviewInfo = $this->coursesReviewsModel->getInfo($this->req["p_course_review_id"]);
            if(!$pCourseReviewInfo){
                return $this->fail("回复的评论不存在");
            }
            $data["to_uid"] = $pCourseReviewInfo["uid"];
            $data["to_user_info"] = json_encode($pCourseReviewInfo["user_info"],256);
        }



        $reviewsRes = $this->coursesReviewsModel->insertGetId($data);

        if($reviewsRes){
            return $this->succ("发表成功");
        }else{
            return $this->fail("发表失败");
        }
    }
}
